package com.example.test_flutter.channel

import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import com.atom.module.logger.Logger
import com.example.test_flutter.BuildConfig
import com.example.test_flutter.R
import io.flutter.embedding.android.FlutterFragment
import io.flutter.plugin.common.*

class MethodChannelActivity : AppCompatActivity() {

    lateinit var flutterFragment: FlutterFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 跳转到flutter
        val createDefault = FlutterFragment.createDefault()
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.add(R.id.fragmentLayout, createDefault)
        beginTransaction.commit()
        flutterFragment = createDefault
    }

    val Method_Channel = "com.example.flutter_test_call/methodChannel"

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            flutterFragment.flutterEngine?.apply {
                Log.e("test", "Have received Test Method Call : 2")
                //1.创建android端的MethodChannel
                val methodChannel = MethodChannel(this.dartExecutor, Method_Channel)
                //2.通过setMethodCallHandler响应Flutter端的方法调用
                methodChannel.setMethodCallHandler { call, result ->
                    //判断调用的方法名
                    if (call.method.equals("testMethodCall")) {
                        //获取传递的参数
                        Log.e("test", "Have received Test Method Call :${call.arguments}")
                        //返回结果给Flutter端
                        result.success(BuildConfig.DEBUG.toString() + "yes")
                    }
                }
            }
        }

        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            flutterFragment.flutterEngine?.apply {
                Log.e("test", "Have received Test Method Call : 2")
                //1.创建android端的MethodChannel
                val methodChannel = MethodChannel(this.dartExecutor, Method_Channel)
                //2.通过setMethodCallHandler响应Flutter端的方法调用
                methodChannel.invokeMethod(
                    "nativeCallFlutterMethod",
                    "asdasdasd",
                    object : MethodChannel.Result {
                        override fun success(result: Any?) {
                            Logger.e("success", result)
                        }

                        override fun error(
                            errorCode: String,
                            errorMessage: String?,
                            errorDetails: Any?
                        ) {

                            Logger.e("success", errorCode, errorMessage, errorDetails)
                        }

                        override fun notImplemented() {

                            Logger.e("notImplemented")
                        }

                    })
                Log.e("test", "Have received Test Method Call : 3")
            }


        }
        return super.onKeyDown(keyCode, event)
    }
}